package io.characters;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

import io.TestResourcesDirectory;

/**
 * 缓冲输入输出字符流的演示
 * 
 * @author fidel
 */
public class BufferedDemo {
    @Test
    public void test() {
        File file = new File(TestResourcesDirectory.PATH + File.separator + "CharactersBufferedDemo.txt");
        try (Writer w = new FileWriter(file); BufferedWriter bw = new BufferedWriter(w);) {
            Scanner input = new Scanner(System.in);
            System.out.println("输入一段文字");
            String txt = input.nextLine();
            input.close();
            int len = 16;
            char[] arr = new char[len];
            Random random = new Random();
            int count = random.nextInt(63) + 2;
            System.out.println(count + "行相同的文字将被写入至另一文本文件");
            for (int n = 0; n < count; n++) {
                for (int i = 0; i < txt.length(); i++) {
                    int mod = i % len;
                    arr[mod] = txt.charAt(i);
                    if (mod == len - 1 || i == txt.length() - 1) {
                        if (mod == len - 1) {
                            // 将数组中的所有字符写入流
                            bw.write(arr);
                        } else {
                            // 将数量不足数组长度的剩余字符写入流
                            bw.write(arr, 0, mod + 1);
                        }
                    }
                }
                // 写入行分隔符
                bw.newLine();
            }
            bw.flush();
            Reader r = new FileReader(file);
            BufferedReader br = new BufferedReader(r);
            arr = new char[len];
            StringBuilder builder = new StringBuilder();
            while (br.ready()) {
                /*
                 * 第一种写入方式
                 * 通过BufferedReader的readLine方法读取一行字符串
                 * readLine读至行末后不会自动换行
                 * 如果要实现换行效果，请在该行字符串末尾添加换行符
                 */
                {
                    builder.append(br.readLine());
                    if (builder.toString() != null) {
                        /*
                         * System类的lineSeparator方法的作用与系统属性“line.separator”类似
                         * JVM会根据操作系统环境，自动添加相应的换行符
                         */
//                        builder.append(System.lineSeparator());
                        builder.append(System.getProperty("line.separator"));
                        System.out.print(builder.toString());
                        builder.delete(0, builder.length());
                    }
                }
                /*
                 * 第二种写入方式
                 * 如果没有到达流的末尾，则一直从流中读取字符至数组中
                 */
//                {
//                    if ((len = br.read(arr)) != -1) {
//                        for (int i = 0; i < len; i++) {
//                            System.out.print(arr[i]);
//                        }
//                    }
//                }
            }
            br.close();
            r.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
